package com.heyun.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heyun.bean.HeyunCustomer;


import com.heyun.bean.HeyunRecord;
import com.heyun.service.impl.CustomerServiceImpl;
import com.heyun.service.impl.HeyunRecordServiceImpl;


/**
 * @author Administrator
 *
 */
@Controller
public class CustomerController {
	
	@Autowired
	private CustomerServiceImpl customerServiceImpl;
	private HeyunRecordServiceImpl recordServiceImpl;
	/**
	 *model modelMap  modelandview 
	 *
	 */
	@RequestMapping("/customer")
	@ResponseBody
	public Map cusList(String pageNum){
		
		Map map=new HashMap();
		
		Page page=PageHelper.startPage(Integer.valueOf(pageNum), 6);
		List<HeyunCustomer> cusList=customerServiceImpl.findCustomer();
 
		int totals=page.getPages();
		System.out.println(totals);
		map.put("cusList", cusList);
		map.put("totalPages", totals);
		 return map; 
		
	}

	@RequestMapping("/addCustomer")
	@ResponseBody
	public Map saveCustomer(HeyunCustomer gh){
		Map map=new HashMap<>();
		int m=customerServiceImpl.saveCustomer(gh);

		 if(m>0){
			 map.put("message", "添加成功");
		 }else{
			 map.put("message", "系统出错，请重新操作");
		 }
		  return map;
		
	}

	@RequestMapping("/findCustomer")
	@ResponseBody
	public Map<String,HeyunCustomer> findCusMes(String cusId){
		Map<String,HeyunCustomer> map=new HashMap<String,HeyunCustomer>();
		
		int id=Integer.parseInt(cusId);
		
		HeyunCustomer cusMes=customerServiceImpl.findCustomerMes(id);
        map.put("cusMes", cusMes);
		 return map; 
	}

	@RequestMapping("/updateCustomer")
	@ResponseBody
	public Map updateCustomer(HeyunCustomer cus){
		Map map=new HashMap();
		int m=customerServiceImpl.updateCustomer(cus);
		 
		if(m>0){
			 map.put("message", "修改成功");
		 }else{
			 map.put("message", "系统出错，请重新操作");
		 }
		  return map;
		
	}
	@RequestMapping("/distribution")
	@ResponseBody
	public Map distribution(HeyunCustomer cus){
		Map map=new HashMap();
		int m=customerServiceImpl.distribution(cus);

		if(m>0){
			 map.put("message", "分配成功");
		 }else{
			 map.put("message", "系统出错，请重新操作");
		 }
		  return map;
		
	}
	@RequestMapping("/selectList")
	@ResponseBody
	public Map<String, List<HeyunCustomer>> selectList(HeyunCustomer customer){
		System.out.println(customer.getCusName());
		Map<String, List<HeyunCustomer>> map1=new HashMap<String, List<HeyunCustomer>>();
		List<HeyunCustomer> selectList=customerServiceImpl.selectList(customer);
		for (HeyunCustomer heYunCustomer : selectList) {
			System.out.println(heYunCustomer.getCusName());
		}
		map1.put("selectList", selectList);
		
		System.out.println(selectList.toString());
		 return map1; 
	}
	@RequestMapping("/insertRecord")
	@ResponseBody
	public Map insertRecord(HeyunRecord record){
		Map map=new HashMap();
		int m=recordServiceImpl.insertRecord(record);

		if(m>0){
			 map.put("message", "分配成功");
		 }else{
			 map.put("message", "系统出错，请重新操作");
		 }
		  return map;
		
	}
}
